package com.example.demo.service;/**
 * ClassName: TimeSlotAvailabilityService
 * Package: com.example.demo.service
 * Description:
 *
 * @Author 理理
 * @Create 2025/3/2 18:15
 * @Version 1.0
 */

import com.example.demo.entity.TimeSlotAvailability;
import com.example.demo.mapper.SubscribeMapper;
import com.example.demo.mapper.TimeSlotAvailabilityMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

/**
 *
 *@title: TimeSlotAvailabilityService
 *@Author: Tangerine
 *@Date: 2025/3/2 18:15
 *@version 1.0
 */
@Service
public class TimeSlotAvailabilityService {

    @Autowired
    private TimeSlotAvailabilityMapper timeSlotAvailabilityMapper;


    //实验室某日期下所有时间段是否可用
    public List<TimeSlotAvailability> confirm(String name, Date start_date, Date end_date){
        List<TimeSlotAvailability> availabilityList = timeSlotAvailabilityMapper.confirm(name,start_date,end_date);
        for(TimeSlotAvailability temp : availabilityList){
            check(temp);
        }
        return availabilityList;
    }

    //实验室某日期下指定时间段是否可用
    public TimeSlotAvailability con(String name, Date date, int time){
        TimeSlotAvailability temp = timeSlotAvailabilityMapper.con(name,date,time);
        return check(temp);
    }

    //判断方法
    public TimeSlotAvailability check(TimeSlotAvailability temp){
        if(temp.getTeam()==0){ //没有班级预约
            if(temp.getPersonal()!=0){ //存在个人预约
                if(temp.getPersonal()<temp.getCount()){ //计算剩余容量（已经预约的应该小于总容量）
                    temp.setSurplus(temp.getCount()-temp.getPersonal());
                }
                //加审核
                if(temp.getSurplus() > 0){ //判断实验室状态
                    temp.setAvailable(true);
                }else {
                    temp.setAvailable(false);
                }
            }else{ //不存在预约
                temp.setSurplus(temp.getCount());
                temp.setAvailable(true);
            }
        }else { //存在班级预约
            if(temp.getTeam()==1){
                temp.setSurplus(0);
                temp.setAvailable(false);
            }else{
                //审核
            }
        }
        return temp;
    }
}
